package prickworlds.graphics;

import org.lwjgl.util.Color;

public class ColorGradient {
	private Color[] colors;

	public ColorGradient(Color[] colors) {
		this.colors = colors;
	}

	public Color get(float pos) {
		pos *= colors.length - 1;
		if (pos >= colors.length - 1)
			pos = colors.length - 1;
		if (pos <= 0)
			pos = 0;

		int index = (int) pos;
		int index2 = index + 1;
		float lerp = pos - index;

		if (index == colors.length - 1)
			return colors[colors.length - 1];

		Color c = lerp(colors[index], colors[index2], lerp);
		return c;
	}

	private Color lerp(Color color, Color color2, float lerp) {
		int red = (int) (color.getRed() * (1f - lerp) + color2.getRed() * lerp);
		int green = (int) (color.getGreen() * (1f - lerp) + color2.getGreen()
				* lerp);
		int blue = (int) (color.getBlue() * (1f - lerp) + color2.getBlue()
				* lerp);
		int alpha = (int) (color.getAlpha() * (1f - lerp) + color2.getAlpha()
				* lerp);
		return new Color(red, green, blue, alpha);
	}
}
